home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7146 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 'Freeing' storage with realloc
  5. Date: Sat, 17 Feb 96 22:27:06 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <824596026snz@genesis.demon.co.uk>
  9. References: <4fv44f$p2v@news.rz.uni-passau.de>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4fv44f$p2v@news.rz.uni-passau.de>
  16.            berndl@sidonius.uni-passau.de "Klaus Berndl" writes:
  17.  
  18. >
  19. >Please look at the snipped of code:
  20. >
  21. >#include <stdio.h>
  22. >
  23. >main () {
  24. >
  25. >  char* buf = NULL;
  26. >
  27. >  buf = (char*)malloc(100);
  28. >  strcpy(buf, "Klaus Berndl");
  29. >  printf("\n%s\n", buf);
  30. >
  31. >/* line 10 */  buf = (char*)realloc(buf, strlen(buf)+1);  /* line 10 */
  32. >
  33. >  if (buf)
  34. >    printf("\n%s\n", buf);
  35. >  else
  36. >    printf("\nshit!\n");
  37. >}
  38. >
  39. >My questions are now: How much memory is allocated for 'buf' after
  40. >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
  41. >Or are still 100 bytes allocated for buf?
  42.  
  43. The extra storage is in principle if not in practice freed. All that is
  44. important as far as the program is concerned is that it is illegal to
  45. access buf[13] or beyond after the realloc. Whether a particular
  46. implementation leaves the storage in place is up to the implementation
  47. although any reasonable implementation will make the space available for
  48. further allocation where possible. If you still need to use the space as
  49. part of that array then that call to realloc is inappropriate and should
  50. be removed or changed to a more suitable value.
  51.  
  52. -- 
  53. -----------------------------------------
  54. Lawrence Kirby | fred@genesis.demon.co.uk
  55. Wilts, England | 70734.126@compuserve.com
  56. -----------------------------------------
  57.